package cn.yeziji.forum.factory.websocket;

import cn.yeziji.forum.factory.websocket.enums.WebsocketFactoryOption;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * websocket 操作工厂
 *
 * @author gzkemays
 * @since 2022/3/27 14:10
 * @deprecated to netty
 */
public class WebsocketOperationFactory {
  static final String INTERFACE_OPERATION = "operation";

  public static Object operation(WebsocketFactoryOption option, Object params) {
    Class<?> clazz = option.getClazz();
    try {
      Constructor<?> constructor = clazz.getConstructor(Object.class);
      Object obj = constructor.newInstance(params);
      Method method = clazz.getDeclaredMethod(INTERFACE_OPERATION, WebsocketFactoryOption.class);
      return method.invoke(obj, option);
    } catch (InvocationTargetException
        | InstantiationException
        | IllegalAccessException
        | NoSuchMethodException e) {
      e.printStackTrace();
    }
    return null;
  }

  public static Object operation(WebsocketFactoryOption option, Object params, Object additional) {
    Class<?> clazz = option.getClazz();
    try {
      Constructor<?> constructor = clazz.getConstructor(Object.class, Object.class);
      Object obj = constructor.newInstance(params, additional);
      Method method = clazz.getDeclaredMethod(INTERFACE_OPERATION, WebsocketFactoryOption.class);
      return method.invoke(obj, option);
    } catch (InvocationTargetException
        | InstantiationException
        | IllegalAccessException
        | NoSuchMethodException e) {
      e.printStackTrace();
    }
    return null;
  }
}
